<template>
  <div class="dashboard">
    <div class="permisson">
      <!-- 头部 -->
      <head-tool>
        <template slot="after">
          <el-button @click="addShow('0', 1)" type="primary"
            >添加权限</el-button
          >
        </template>
      </head-tool>
      <!-- 表格 -->
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="code" label="标识"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="description" label="操作">
          <template slot-scope="{ row }">
            <!-- 表格里面，只有二级权限才能有添加按钮 -->
            <el-button
              v-if="row.type == 1"
              @click="addShow(row.id, 2)"
              type="text"
              >添加</el-button
            >
            <el-button @click="doEdit(row.id)" type="text">编辑</el-button>
            <el-button @click="doDel(row.id)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
      >
        <el-form :model="formData" label-width="80px">
          <el-form-item prop="name" label="权限名称">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item prop="code" label="权限标识">
            <el-input v-model="formData.code"></el-input>
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description"></el-input>
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-row type="flex" justify="center">
            <el-button @click="save" size="mini" type="primary">确定</el-button>
            <el-button @click="cancel" size="mini">取消</el-button>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 导入头部组件
import headTool from '@/components/headTool'
// 导入接口
import {
  getPermissionList,
  addPermission,
  removePermission,
  getPermissionDetail,
  updatePermissionDetail
} from '@/api/permission'
// 导入树状递归函数
import { getDepData } from '@/utils'
import { title } from '@/settings'
export default {
  name: 'permission',
  components: {
    headTool
  },
  data () {
    return {
      // 表格数据
      tableData: [],
      showDialog: false,
      // 对话框表单数据
      formData: {}
    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList () {
      let res = await getPermissionList()
      // console.log(res)
      this.tableData = getDepData(res.data, '0')
    },
    // 添加的点击事件：
    addShow (pid, type) {
      this.showDialog = true
      // 点击每一行的添加按钮时，把这一行的pid/type（区分一级二级权限）传过来，保存下来方便新增的确认按钮传参发请求！
      this.formData.pid = pid
      this.formData.type = type
    },
    // 确认
    async save () {
      // 编辑的确认
      if (this.formData.id) {
        let res1 = await updatePermissionDetail(this.formData)
        // console.log(res1)
      } else {
        // 新增的确认
        let res = await addPermission(this.formData)
        // console.log(res)
      }
      // 提示、重新加载列表、隐藏对话框
      this.$message(this.title + '成功！')
      this.showDialog = false
      this.getPermissionList()
      this.formData = {}
    },
    // 取消
    cancel () {
      this.showDialog = false
      this.formData = {}
    },
    // 编辑的点击事件
    async doEdit (id) {
      // 先获取所有的权限点
      let res = await getPermissionDetail(id)
      // console.log(res)
      this.showDialog = true
      this.formData = res.data
    },
    // 删除的点击事件
    async doDel (id) {
      let res = await removePermission(id)
      this.$message.success('删除成功！')
      this.getPermissionList()
    }
  },
  computed: {
    title () {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  }
}
</script>

<style scoped></style>
